/*
 * SecurityFilter.java
 *
 * Created on 23 de Junho de 2007, 11:57
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.unifor.finance.presentation.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Leonardo
 */
public class SecurityFilter implements Filter {
  
  private HttpSession session;
  
  public void init(FilterConfig filterConfig) throws ServletException {
  }
  
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    
    session = httpRequest.getSession(false);
    
    if(session != null && session.getAttribute("usuario") != null)
      chain.doFilter(request, response);
    else
      httpResponse.sendRedirect("index.login");
      //httpRequest.getRequestDispatcher("index.login").forward(request, response);      
  }
  
  public void destroy() {
  }
  
}
